﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataBaseLibrary.DBInterfaces;
using System.ComponentModel;

namespace DataBaseLibrary.DBObjectsFB
{
    public class IndividualAddress :  DBFObject, IDBObject, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }


        String m_City = "";
        /// <summary>
        /// Город
        /// </summary>
        public string City 
        {
            set
            {
                m_City = value;
                OnPropertyChanged(new PropertyChangedEventArgs("City"));
            }

            get
            {
                return m_City;
            }
        }

        /// <summary>
        /// Улица
        /// </summary>
        String m_Street = "";
        public string Street
        {
            set
            {
                m_Street = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Street"));
            }

            get
            {
                return m_Street;
            }
        }

        String m_NumberBuilding = "";
        /// <summary>
        /// Корпус
        /// </summary>
        public string NumberBuilding 
        {
            set
            {
                m_NumberBuilding = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NumberBuilding"));
            }

            get
            {
                return m_NumberBuilding;
            }
        }

        String m_NumberFlat = "";
        /// <summary>
        /// Квартира
        /// </summary>
        public string NumberFlat 
        {
            set
            {
                m_NumberFlat = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NumberFlat"));
            }

            get
            {
                return m_NumberFlat;
            }
        }

        String m_NumberHome = "";
        /// <summary>
        /// Дом
        /// </summary>
        public string NumberHome 
        {
            set
            {
                m_NumberHome = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NumberHome"));
            }

            get
            {
                return m_NumberHome;
            }
        }

        String m_ResidentialArea = "";
        /// <summary>
        /// Область
        /// </summary>
        public string ResidentialArea 
        {
            set
            {
                m_ResidentialArea = value;
                OnPropertyChanged(new PropertyChangedEventArgs("ResidentialArea"));
            }

            get
            {
                return m_ResidentialArea;
            }
        }

        public IndividualAddress()
        {
            NameTable = "INDIVIDUAL_ADDRESS_TABLE";
            City = ""; 
            Street = ""; 
            NumberBuilding = ""; 
            NumberFlat = ""; 
            NumberHome = ""; 
            ResidentialArea = "";
        }

        private void CreateData()
        {
            Parameters = new Dictionary<string, object>();
            Parameters.Add("CITY", City);
            Parameters.Add("RESIDENTIAL_AREA", ResidentialArea);
            Parameters.Add("STREET", Street);
            Parameters.Add("NUMBER_HOME", NumberHome);
            Parameters.Add("NUMBER_BUILDING", NumberBuilding);
            Parameters.Add("NUMBER_FLAT", NumberFlat);

        }

        object IDBObject.Save()
        {
            try
            {
                CreateData();
                return SaveObject();
            }
            catch(SystemException)
            {
                throw new SystemException(String.Format("Ошибка при сохранении объекта {0} в БД", this.ToString()));                
            }
        }

        void IDBObject.Load(object ID)
        {
            try
            {
                CreateData();
                LoadObject(ID);

                City = Parameters["CITY"] as String;
                Street = Parameters["STREET"] as String;
                NumberBuilding = Parameters["NUMBER_BUILDING"] as String;
                NumberFlat = Parameters["NUMBER_FLAT"] as String;
                NumberHome = Parameters["NUMBER_HOME"] as String;
                ResidentialArea = Parameters["RESIDENTIAL_AREA"] as String;

            }
            catch (SystemException)
            {
                throw new SystemException(String.Format("Ошибка при загрузке объекта {0} в БД", this.ToString()));
            }
        }




        void IDBObject.Update()
        {
            CreateData();
            UpdateObject();
        }
    }
}
